<?php

	include("config.php");
    include(INCLUDEDIR."core.php");
	include(INCLUDEDIR."class.emailtpl.php");
	
	$tpl = new Sitio();
	$tpl->setPlantilla("tpl.registro.html");
	$tpl->prepare();
	$tpl->newBlock("HEADERPRINCIPAL");
	
		
	$email = isset($_POST['email'])?sanitizar($_POST['email']):"";
	$captcha = isset($_POST['captcha'])?strtolower($_POST['captcha']):"";

	if($_POST['accion']=='recordar') {		
		$error = array();
		
		if(empty($email)) $error[] = "Debe indicar una direcci&oacute;n de correo";
		if(!esEmail($email) && !empty($email)) $error[] = "El correo debe ser valido";
		if(count($error)==0) {
			$fila = db::getRow("SELECT * FROM personas WHERE email = '$email'");
			if(count($fila)==0) $error[] = "La direcci&oacute;n indicada no existe";
		}		
		if($captcha!=$_SESSION['captcha']) $error[] = "Debe escribir el codigo que aparece en la imagen";
		if(count($error)>0) {
			$tpl->newBlock("ERRORBLOCK");
			foreach($error as $e) {
				$tpl->newBlock("ERRORITEM");
				$tpl->assign("error",$e);
			}
		} else {
			$password = randomText(6);
			$hash = md5($password);
			mysql_query("UPDATE personas SET password = '$hash' WHERE email = '$email'");
			
				$salida = new Emailtpl();			
				$salida->Load("recordar_clave.html");
				$salida->setTitle("Recordatorio de clave");
				$fila = db::getRow("SELECT * FROM personas WHERE email = '$email'");
				foreach($fila as $k => $v) $salida->set($k,$v);
				$salida->set("password",$password);				
				//$email->setAddress($usuario_email,$usuario_nombre);
				$salida->setAddress($email,$fila['nombre_persona']);
				$salida->Send();				
				//redir($_SERVER['PHP_SELF']."?accion=poractivar");
				$tpl->newBlock("RECORDATORIOENVIADO");
				$tpl->Mostrar();
			//
		}
	}
	$tpl->newBlock("RECORDARCLAVE");
	
	$tpl->Mostrar();
	

?>